package com.vive.user;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.*;
import org.apache.wicket.util.lang.Bytes;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.validator.StringValidator;


public class UserEditPage extends WebPage{
	private static List favorites = new ArrayList();
	private static List jobs = new ArrayList();
	private static List workYears = new ArrayList();
	private static List marraige = new ArrayList();

	private static List female = new ArrayList();

	private FileUploadField image;
	
	static {
		favorites.add("movie");
		favorites.add("read");
		favorites.add("travel");
		
		for(int i = 0;i<10;i++) {
			workYears.add(new Integer(i));
		}
		
		jobs.add("actors");
		jobs.add("teacher");
		jobs.add("student");
		jobs.add("reporter");
		
		marraige.add(Boolean.FALSE);
		marraige.add(Boolean.TRUE);

		female.add("男");
		female.add("女");

	}
	
	public UserEditPage() {
		super();
		User user = new User();
		
		add(new FeedbackPanel("feedback"));

		image = new FileUploadField("image");

		Form form = new Form("form", new CompoundPropertyModel(user)) {
			private static final long serialVersionUID = 1L;

			protected void onSubmit() {
				final FileUpload upload = image.getFileUpload();
				if (upload != null) {
					try {
						upload.writeTo(new File("d:\\2.txt"));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				super.onSubmit();
				System.out.println("onSubmit");
			}
		};
		form.add(image);
		form.setMultiPart(true);
		form.setMaxSize(Bytes.kilobytes(1000));

		add(form);

		TextField accountTextField = new TextField("account");
		//1.添加长度验证
		accountTextField.setRequired(true);
		accountTextField.add(StringValidator.lengthBetween(6,20));


		form.add(accountTextField);
		form.add(new TextField("name"));
		form.add(new PasswordTextField("password"));
		
//		WebMarkupContainer birthdayContainer = new WebMarkupContainer("birthdayContainer");
//		form.add(birthdayContainer);
		TextField birthdayTextField = new TextField("birthday",Date.class);
		form.add(birthdayTextField);
//		form.add(new DatePicker());
		
		form.add(new TextField("salary",Double.class));
		form.add(new DropDownChoice("job",jobs));
		form.add(new ListChoice("workYear",workYears));
		form.add(new RadioChoice("married",marraige));
		form.add(new CheckBoxMultipleChoice("favorites",favorites));
		form.add(new CheckBox("female"));



	}

}
